package com.mjf.web.controller.system;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mjf.common.core.controller.BaseController;
import com.mjf.common.core.domain.AjaxResult;
import com.mjf.system.domain.MjfViewDes;
import com.mjf.system.service.IMjfViewDesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * desc
 * </p>
 *
 * @author chenshoumeng
 * @date 2024/11/22 19:43
 */
@RestController
@RequestMapping("/mjf/view/des")
public class MjfViewDesController extends BaseController {

    @Autowired
    private IMjfViewDesService iMjfViewDesService;

    @GetMapping("")
    public AjaxResult viewDes(@RequestParam("viewId") String viewId)
    {
        iMjfViewDesService.testPage();
        return success(iMjfViewDesService.list(Wrappers.<MjfViewDes>lambdaQuery().eq(MjfViewDes::getViewId, viewId)));
    }

    @PostMapping()
    public AjaxResult add(@RequestBody MjfViewDes mjfViewDes)
    {
        List<MjfViewDes> des;
        if (ObjectUtils.isNotNull(mjfViewDes.getId()))
        {
            des= iMjfViewDesService.list(Wrappers.<MjfViewDes>lambdaQuery()
                .eq(MjfViewDes::getViewId, mjfViewDes.getViewId())
                .ne(MjfViewDes::getId, mjfViewDes.getId())
            );
            if (CollectionUtils.isNotEmpty(des))
            {
                return error("页面ID重复");
            }
            des= iMjfViewDesService.list(Wrappers.<MjfViewDes>lambdaQuery()
                .eq(MjfViewDes::getViewName, mjfViewDes.getViewName())
                .ne(MjfViewDes::getId, mjfViewDes.getId())
            );
            if (CollectionUtils.isNotEmpty(des))
            {
                return error("页面名称重复");
            }
        } else {
            des= iMjfViewDesService.list(Wrappers.<MjfViewDes>lambdaQuery()
                .eq(MjfViewDes::getViewId, mjfViewDes.getViewId())
            );
            if (CollectionUtils.isNotEmpty(des))
            {
                return error("页面ID重复");
            }
            des= iMjfViewDesService.list(Wrappers.<MjfViewDes>lambdaQuery()
                .eq(MjfViewDes::getViewName, mjfViewDes.getViewName())
            );
            if (CollectionUtils.isNotEmpty(des))
            {
                return error("页面名称重复");
            }
        }
        iMjfViewDesService.saveOrUpdate(mjfViewDes);
        return success();
    }

}
